﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.Client.Common;

namespace OpenSoftStore.Client.UIL.UserControls
{
    public class YhSoftTreeView:TreeView
    {
        public List<TreeNode> TreeNodes;
        public string TypeId;
        public string TypeText;
        public Dictionary<string, string> Pys = new Dictionary<string, string>();
        public string PySelect = "";

        public YhSoftTreeView()
        {
            TreeNodes=new List<TreeNode>();
            AfterSelect += TvAfterSelect;
            KeyUp += trTree_KeyUp;
            Enter += trTree_Enter;
            Leave += trTree_Leave;
            //trTree_Enter(null, null);
        }

        private void trTree_Enter(object sender, EventArgs e)
        {
            CheckAllChildNodes(Nodes[0], TreeNodes);
            if (Pys.Count == 0)
            {
                foreach (var treeNode in TreeNodes)
                {
                    var text = treeNode.Text;
                    var py = CnToPy.CapitalCovert(text);
                    //treeNode.ToolTipText = py ;
                    //treeNode.s
                    //treeNode.Text = treeNode.Text + "(" + py + ")";
                    SetPy(py, text);
                }
            }
        }
        private void trTree_Leave(object sender, EventArgs e)
        {
            var tv = sender as TreeView;
            if (tv.SelectedNode == null) return;
            tv.SelectedNode.BackColor = Color.FromArgb(255, 49, 106, 197);
            tv.SelectedNode.ForeColor = Color.White;
            //CheckAllChildNodes(Nodes[0], TreeNodes);
            foreach (TreeNode treeNode in TreeNodes)
            {
                if (treeNode.Text == tv.SelectedNode.Text) continue;
                treeNode.BackColor = tv.BackColor;
                treeNode.ForeColor = Color.Black;
            }
          
        }

        public void TvAfterSelect(object sender, TreeViewEventArgs e)
        {
            //var tv = sender as TreeView;
            //if (TreeNodes.Count==0) CheckAllChildNodes(Nodes[0], TreeNodes);
            var tv = sender as TreeView;
            if (tv.SelectedNode == null) return;
            //CheckAllChildNodes(Nodes[0], TreeNodes);
            foreach (TreeNode treeNode in TreeNodes)
            {
                if (treeNode.Text == tv.SelectedNode.Text) continue;
                treeNode.BackColor = tv.BackColor;
                treeNode.ForeColor = Color.Black;
            }
            tv.SelectedNode.BackColor = Color.FromArgb(255, 49, 106, 197);
            tv.SelectedNode.ForeColor = Color.White;
            //if (tv.SelectedNode == tv.Nodes[0])
            //{
            //    tv.SelectedNode.BackColor = Color.FromArgb(255, 49, 106, 197);
            //    tv.SelectedNode.ForeColor = Color.White;
            //}
            //else
            //{
            //    tv.Nodes[0].BackColor = tv.BackColor;
            //    tv.Nodes[0].ForeColor = Color.Black;
            //    tv.SelectedNode.BackColor = tv.BackColor;
            //    tv.SelectedNode.ForeColor = Color.Black;
            //}
        }

        private void trTree_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Subtract || e.KeyCode == Keys.Back)
            {
                PySelect = "";
            }
            if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z))
            {
                PySelect += e.KeyCode;
            }
            if (!string.IsNullOrEmpty(PySelect))
            {
                var text = GetPy(PySelect);
                if (!string.IsNullOrEmpty(text)) SelectTreeView(this, text);
            }
        }

        private void SelectTreeView(TreeView treeView, string selectStr)
        {
            treeView.Focus();
            foreach (var treeNode in TreeNodes)
            {
                if (treeNode.Text == selectStr)
                {
                    treeView.SelectedNode = treeNode;//选中
                    return;
                }
            }
        }

        private void SetPy(string key, string text)
        {
            if (!Pys.Any(m => m.Key == key))
            {
                Pys.Add(key, text);
            }
        }

        private string GetPy(string pySelect)
        {
            var py = Pys.FirstOrDefault(m => m.Key==pySelect);
            var count = Pys.Count(m => m.Key==pySelect);
            if (count == 1) PySelect = "";
            if (string.IsNullOrEmpty(py.Key))
            {
                //PySelect = "";
                return "";
            }
            return Pys[py.Key];
        }

        private void CheckAllChildNodes(TreeNode treeNode, List<TreeNode> treeNodes)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                //var tree = new TreeNode { Name = node.Text, Id = node.Name };
                treeNodes.Add(node);
                if (node.Nodes.Count > 0)
                {
                    this.CheckAllChildNodes(node, treeNodes);
                }
            }
        }



    }
}
